package com.cma.service.impl;

import com.cma.dao.TcnameRepository;
import com.cma.model.db.*;
import com.cma.dao.HistctrackRepository;
import com.cma.model.param.HistctrackParam;
import com.cma.model.param.Lonlat;
import com.cma.model.resultdb.DateTf;
import com.cma.model.resultdb.SimpleTf;
import com.cma.service.HistctrackService;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import java.util.*;


/**
 * 台风中心路径
 * 数据库表【histctrack】对应的service实现类
 *
 * @author bjs
 * @date 2017-01-03 18:59:18
 */
@Service
public class HistctrackServiceImpl implements HistctrackService {

    private static final Log logger = LogFactory.getLog(HistctrackServiceImpl.class);

    private static final String TF_CENTER_REALTIME = "BABJ";
    //    private static final String TF_CENTER_HIS = "BJ";
    private static final String FCSTType = "BABJWTPQ";
    private static final int FCST_HOUT = 0;

    private final static Map TF_STRENTH_VALUE = new HashMap();

    static {
        TF_STRENTH_VALUE.put("TD", new double[]{10.8,17.1});
        TF_STRENTH_VALUE.put("TS", new double[]{17.2,24.4});
        TF_STRENTH_VALUE.put("STS",  new double[]{24.5,32.6});
        TF_STRENTH_VALUE.put("TY",  new double[]{32.7,41.4});
        TF_STRENTH_VALUE.put("STY",  new double[]{41.5,50.9});
        TF_STRENTH_VALUE.put("SuperTY",  new double[]{51,9999});
    }
    @Autowired
    private HistctrackRepository histctrackRepository;

    @Autowired
    private TcnameRepository tcnameRepository;

    @PersistenceContext
    private EntityManager entityManager;


    //===================================================================================================

    /**
     * 保存或更新对象
     *
     * @param histctrack
     */
    @Override
    public void save(Histctrack histctrack) {
        histctrackRepository.save(histctrack);
    }


    /**
     * 通过id查找对象
     *
     * @param id
     * @return
     */
    @Override
    public Histctrack findById(Long id) {
        return histctrackRepository.findOne(id);
    }

    @Override
    public List<Histctrack_Tcname> test1(int i) {
      /* // entityManager.createQuery()
        try{
            String sql="select a.*,b.engname,b.chnname from histctrack a left join tcname b on a.name=b.engname  where a.tfxh="+i+"";
            StringBuffer sqls=new StringBuffer(sql);

            Query queryForCount = entityManager.createNativeQuery(sqls.toString(), Histctrack_Tcname.class);
            List<Histctrack_Tcname> adminList=queryForCount.getResultList();
            return adminList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;*/
        return null;

    }

    /**
     * 根据参数据查询台风信息
     *
     * @param histctrackParam
     * @return
     */
    public List<SimpleTf> queryTfByParams(HistctrackParam histctrackParam) {
        // TODO 查询所有的台风名字数据，用于后面台风数据处理使用
        Map<String, Tcname> tcnameMap = getStringTcnameMap();
        // TODO 根据ID查询,tfbh查询不与其他条件同时组合查询，tfbh唯一确定台风
        // bjs 注释掉2017-3-10：原因多边形查询的时候先有了台风编号其他条件失效！！！！
//        String tfbhs = histctrackParam.getTfbhs();
//        if (null != tfbhs && !"".equals(tfbhs)) {
//            return qeuryByTfbh(tfbhs, tcnameMap);
//        }
        // TODO tfbh以外的条件组合查询:经纬度、时间、台风登陆信息
        // 根据MySQL查询条件执行顺序（从左到右），将快速缩小查询范围的条件放在前面
        return queryByMutilParams(histctrackParam, tcnameMap);

    }

    /**
     * tfbh以外的条件组合查询:经纬度、时间、台风登陆信息
     *
     * @param histctrackParam
     * @param tcnameMap
     * @return
     */
    private List<SimpleTf> queryByMutilParams(HistctrackParam histctrackParam, Map<String, Tcname> tcnameMap) {
        StringBuffer strBufReal = new StringBuffer("select DISTINCT t.tfbh as tfbh,'new' as ty ,t.engname as name,if(left(t.tfbh,2)>'48',CONCAT('19',t.tfbh),CONCAT('20',t.tfbh)) as  tfhbb   from tcrealtime t where 1=1 ");
        StringBuffer strBufHis = new StringBuffer("select DISTINCT t.tfbh as tfbh,'his' as ty,t.name as name,if(left(t.tfbh,2)>'48',CONCAT('19',t.tfbh),CONCAT('20',t.tfbh)) as  tfhbb  from histctrack t where 1=1 ");
        strBufReal.append(" and t.center='" + TF_CENTER_REALTIME + "' and  t.fcsthour=" + FCST_HOUT + " and  t.FCSTType='" + FCSTType + "' ");

        // 破代码怎么改？bjs-----------------
//        if (!StringUtils.isEmpty(histctrackParam.getLonlatList())) {
            addTfbhSqlStr(strBufReal, strBufHis, histctrackParam);
//        }
        //----------------end----------------

        //TODO 1、根据登录信息查询：登陆地点、登陆城市、登陆强度。。。
        addCitySqlStr(strBufReal, strBufHis, histctrackParam);
        addLandinfoSqlStr(strBufReal, strBufHis, histctrackParam);
        //TODO 2、根据时间查询
        addTimeSqlStr(strBufReal, strBufHis, histctrackParam);
        //TODO 3、根据经纬度范围：矩形框、圆（中心、半径）
        addLatlngSqlStr(strBufReal, strBufHis, histctrackParam);
        strBufReal.append(" ORDER by datetime desc ");
        strBufHis.append(" ORDER BY datetime desc");
        Query queryForReal = entityManager.createNativeQuery(strBufReal.toString());
        Query queryForHis = entityManager.createNativeQuery(strBufHis.toString());

        List<Object[]> listReal = queryForReal.getResultList();
        List<Object[]> listHis = queryForHis.getResultList();
        listReal.addAll(listHis);
        List<SimpleTf> resultList = getSimpleTfs(tcnameMap, listReal);
        return resultList;
    }

    private void addTfbhSqlStr(StringBuffer strBufReal, StringBuffer strBufHis, HistctrackParam histctrackParam) {
        String tfbhs = histctrackParam.getTfbhs();
        if (null != tfbhs && !"".equals(tfbhs)) {
            tfbhs=tfbhs.replace(";",",");
            tfbhs=tfbhs.replace("；",",");
            strBufReal.append(" and t.tfbh in (" + tfbhs + ")");
            strBufHis.append(" and t.tfbh in (" + tfbhs + ")");
        }else {
            return;
        }
    }

    /**
     * 根据登录信息查询：登陆强度、风
     *
     * @param strBufReal
     * @param strBufHis
     * @param histctrackParam
     */
    private void addLandinfoSqlStr(StringBuffer strBufReal, StringBuffer strBufHis, HistctrackParam histctrackParam) {
        //如果是登陆前的话
        if (!StringUtils.isEmpty(histctrackParam.getDlbefore()) && histctrackParam.getDlbefore().equals("true")) {
            String sqlDl = "select c.c_tfbh from tflandinginfo c where 1=1 ";

            //TODO 强度等级  貌似没有字段
            if (histctrackParam.getStrengthOrValue().equals("1")) {
                String[] strengthStr = getStrengthString(histctrackParam).split(",");
                sqlDl += " and (   ";
                for(int i=0;i<strengthStr.length;i++){
                    if(i>0){
                        sqlDl +=" or ";
                    }
                    double[] vals = (double[]) TF_STRENTH_VALUE.get(strengthStr[i].replace("'",""));
                    sqlDl += "  (c.c_windspeed >= "+vals[0]+" and c.c_windspeed <= "+vals[1]+" )";
                }
                sqlDl += " ) ";
            } else {
                //根据气压 或者气压范围
                if (!StringUtils.isEmpty(histctrackParam.getMaxpressure()) || !StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                    if (!StringUtils.isEmpty(histctrackParam.getMaxpressure())) {
                        sqlDl += " and c.v_centerpres<=" + histctrackParam.getMaxpressure();
                    }
                    if (!StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                        sqlDl += " and c.v_centerpres>=" + histctrackParam.getMinpressure();
                    }
                }
                //风速
                if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity()) || !StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                    if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity())) {
                        sqlDl += " and c.c_windspeed<='" + histctrackParam.getMaxvelocity() + "'";
                    }
                    if (!StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                        sqlDl += " and c.c_windspeed>='" + histctrackParam.getMinvelocity() + "'";
                    }
                }
            }
            strBufReal.append(" and t.tfbhbabj in( ");
            strBufReal.append(sqlDl);
            strBufReal.append(" )");
            strBufHis.append(" and t.tfbh in(");
            strBufHis.append(sqlDl);
            strBufHis.append(" )");
        } else if((!StringUtils.isEmpty(histctrackParam.getDlbefore()) && histctrackParam.getDlbefore().equals("false"))){
            if (histctrackParam.getStrengthOrValue().equals("1")) {
                if (!StringUtils.isEmpty(histctrackParam.getStrength())) {
                    String strengthStr = getStrengthString(histctrackParam);
                    strBufReal.append(" and t.strength in(" + strengthStr + ")");
                    strBufHis.append(" and t.strength in(" + strengthStr + ")");
                }
            } else if(histctrackParam.getStrengthOrValue().equals("2")){
                //气压
                if (!StringUtils.isEmpty(histctrackParam.getMaxpressure()) || !StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                    if (!StringUtils.isEmpty(histctrackParam.getMaxpressure())) {
                        strBufReal.append(" and t.pressure<='" + histctrackParam.getMaxpressure() + "'");
                        strBufHis.append(" and t.pressure<='" + histctrackParam.getMaxpressure() + "'");
                    }
                    if (!StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                        strBufReal.append(" and t.pressure>='" + histctrackParam.getMinpressure() + "'");
                        strBufHis.append(" and t.pressure>='" + histctrackParam.getMinpressure() + "'");
                    }
                }
                //风速范围
                if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity()) || !StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                    if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity())) {
                        strBufReal.append(" and t.windv<='" + histctrackParam.getMaxvelocity() + "'");
                        strBufHis.append(" and t.wind<='" + histctrackParam.getMaxvelocity() + "'");
                    }
                    if (!StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                        strBufReal.append(" and t.windv>='" + histctrackParam.getMinvelocity() + "'");
                        strBufHis.append(" and t.wind>='" + histctrackParam.getMinvelocity() + "'");
                    }
                }
            }
        }
    }

    private String getStrengthString(HistctrackParam histctrackParam) {
        String strengthStr = "";
        String[] strs = histctrackParam.getStrength().split(",");

        for (String s : strs) {
            if (!StringUtils.isEmpty(s)) {
                if(strengthStr.length()>0){
                    strengthStr = "'" + s + "',"+strengthStr;
                }else{
                    strengthStr="'"+s+"'";
                }

            }
        }
        return strengthStr;
    }

    /**
     * 根据经纬度范围：矩形框、圆（中心、半径）
     *
     * @param strBufReal
     * @param strBufHis
     * @param histctrackParam
     */
    private void addLatlngSqlStr(StringBuffer strBufReal, StringBuffer strBufHis, HistctrackParam histctrackParam) {
        if (histctrackParam != null && !StringUtils.isEmpty(histctrackParam.getLatlngType())) {
            String latlngType = histctrackParam.getLatlngType();
            String minLon = null;
            String maxLon = null;
            String minLat = null;
            String maxLat = null;
            if (latlngType.equals("1")) {
                //确定经纬度范围，缩小查询范围

                // 计算方式待确定先不用
//                Double lon = Double.parseDouble(histctrackParam.getLon());
//                Double lat = Double.parseDouble(histctrackParam.getLat());
//                Double radius = Double.parseDouble(histctrackParam.getLlWidth())*1000.0;
//                Double lonDelta = radius / 111320.0;
//                Double latDelta = radius/111140.0;
//                minLon = String.valueOf(lon - lonDelta);
//                maxLon = String.valueOf(lon + lonDelta);
//                minLat = String.valueOf(lat - latDelta);
//                maxLat = String.valueOf(lat + latDelta);

            } else if (latlngType.equals("2")) {
                //经纬度范围(矩形框)
                if (!StringUtils.isEmpty(histctrackParam.getMinlat())) {
                    minLat = histctrackParam.getMinlat();
                }
                if (!StringUtils.isEmpty(histctrackParam.getMinlon())) {
                    minLon = histctrackParam.getMinlon();
                }
                if (!StringUtils.isEmpty(histctrackParam.getMaxlat())) {
                    maxLat = histctrackParam.getMaxlat();
                }
                if (!StringUtils.isEmpty(histctrackParam.getMaxlon())) {
                    maxLon = histctrackParam.getMaxlon();
                }
            }
            if (!StringUtils.isEmpty(minLat)) {
                strBufReal.append(" and t.lat>=" + minLat);
                strBufHis.append(" and t.lat>=" + minLat);
            }
            if (!StringUtils.isEmpty(minLon)) {
                strBufReal.append(" and t.lon>=" + minLon);
                strBufHis.append(" and t.lon>=" + minLon);
            }
            if (!StringUtils.isEmpty(maxLat)) {
                strBufReal.append(" and t.lat<=" + maxLat);
                strBufHis.append(" and t.lat<=" + maxLat);
            }
            if (!StringUtils.isEmpty(maxLon)) {
                strBufReal.append(" and t.lon<=" + maxLon);
                strBufHis.append(" and t.lon<=" + maxLon);
            }

            if (latlngType.equals("1")
                    && !StringUtils.isEmpty(histctrackParam.getLlWidth())
                    && !StringUtils.isEmpty(histctrackParam.getLon())
                    && !StringUtils.isEmpty(histctrackParam.getLat())) {
                // 中心点和半径（圆圈）,加在最后
                String latlngSql = " and sqrt((((" + histctrackParam.getLon() + "-t.lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+t.lat)/2)*PI()/180)/180)*" +
                        "((" + histctrackParam.getLon() + "-t.lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+t.lat)/2)*PI()/180)/180))+" +
                        "(((" + histctrackParam.getLat() + "-t.lat)*PI()*12656/180)*((" + histctrackParam.getLat() + "-t.lat)*PI()*12656/180)))<" + histctrackParam.getLlWidth();
                strBufReal.append(latlngSql);
                strBufHis.append(latlngSql);
            }
        }
    }

    /**
     * 根据时间查询
     *
     * @param strBufReal
     * @param strBufHis
     * @param histctrackParam
     */
    private void addTimeSqlStr(StringBuffer strBufReal, StringBuffer strBufHis, HistctrackParam histctrackParam) {
        if (!StringUtils.isEmpty(histctrackParam.getBeginTime()) || !StringUtils.isEmpty(histctrackParam.getEndTime())) {
            String begin = histctrackParam.getBeginTime();
            String end = histctrackParam.getEndTime();
            if (!StringUtils.isEmpty(begin)) {
                strBufReal.append(" and t.datetime >='" + begin + "'");
                strBufHis.append(" and t.datetime >='" + begin + "'");
            }
            if (!StringUtils.isEmpty(end)) {
                strBufReal.append(" and t.datetime <='" + end + "'");
                strBufHis.append(" and t.datetime <='" + end + "'");
            }
        }
        if(!StringUtils.isEmpty(histctrackParam.getYear())){//如果年份不为空的话
            String year=histctrackParam.getYear();
            String yearbg = year + "-01-01 00:00:00";
            String yeared = year + "-12-31 23:59:59";
            strBufReal.append(" and t.datetime >='" + yearbg + "' and t.datetime<='"+yeared+"' ");
            strBufHis.append(" and t.datetime >='" + yearbg + "' and t.datetime<='"+yeared+"' ");
        }

    }


    /**
     * 根据台风编号查询台风，
     *
     * @param tfbhs     一个或者多个编号，以 分号<code>;</code>分割
     * @param tcnameMap
     * @return
     */
    public List<SimpleTf> qeuryByTfbh(String tfbhs, Map<String, Tcname> tcnameMap) {
        //考虑有可能输入中文分号，先处理中文分号,最后替换为逗号 ','可以直接用于数据库查询
        tfbhs = tfbhs.trim();
        if (tfbhs.contains(";")) {
            tfbhs = tfbhs.replace(";", ",");
        }
        if (tfbhs.contains("；")) {
            tfbhs = tfbhs.replace("；", ",");
        }
       // tfbhs.replace("；", ";").replace(";", ",");

        StringBuffer strBufReal = new StringBuffer("select DISTINCT t.tfbh as tfbh,'new' as ty ,t.engname as name  from tcrealtime t where 1=1");
        StringBuffer strBufHis = new StringBuffer("select DISTINCT t.tfbh as tfbh,'his' as ty,t.name as name from histctrack t where 1=1 ");
        strBufReal.append(" and t.center='" + TF_CENTER_REALTIME + "' and  t.fcsthour=" + FCST_HOUT + " and  t.FCSTType='" + FCSTType + "' ");

        strBufReal.append(" and t.tfbh in (" + tfbhs + ")");
        strBufHis.append(" and t.tfbh in (" + tfbhs + ")");

        Query queryForReal = entityManager.createNativeQuery(strBufReal.toString());
        Query queryForHis = entityManager.createNativeQuery(strBufHis.toString());

        List<Object[]> listReal = queryForReal.getResultList();
        List<Object[]> listHis = queryForHis.getResultList();
        listReal.addAll(listHis);
        List<SimpleTf> resultList = getSimpleTfs(tcnameMap, listReal);
        return resultList;
    }

    @Override
    public List<DateTf> findPathDatas(String oldbh) {
        StringBuffer sb = new StringBuffer("select a.*,b.chnname  from histctrack a left join tcname b on a.name=b.engname where a.tfbh in("+oldbh+")");
        sb.append("order by  a.datetime ");
        Query queryForCount1 = entityManager.createNativeQuery(sb.toString(), Histctrack_Tcname.class);
        List<Histctrack_Tcname> list2 = queryForCount1.getResultList();

        List<DateTf> resultList = new ArrayList<DateTf>();
        // 处理数据
        int n = list2.size();
        //整合为与实时库一样的字段名称 维持统一性
        for (int i = 0; i < n; i++) {
            Histctrack_Tcname t = list2.get(i);
            DateTf dif = new DateTf();
            dif.setId(t.getId());
            dif.setChnname(t.getChnname());
            dif.setTfbh(t.getTfbh());
            dif.setCenter(t.getCenter());
            dif.setDatetime(t.getDatetime());
            dif.setCreateTime(t.getCreateTime());
            dif.setEngname(t.getName());
            dif.setCreateTime(t.getCreateTime());
            dif.setFcsthour(0);
            dif.setFCSTType("");
            dif.setTfbhbabj("");
            dif.setTfxh(t.getTfxh());
            dif.setLat(t.getLat());
            dif.setLon(t.getLon());
            dif.setMemo(t.getMemo());
            dif.setMovedir(t.getDirection());
            dif.setPressure(t.getPressure());
            dif.setStrength(t.getStrength());
            dif.setTsCreated(t.getCreateTime());
            dif.setWind7class(t.getWind7class());
            dif.setWind7v1(t.getWind71());
            dif.setWind7v2(t.getWind72());
            dif.setWind7v3(t.getWind73());
            dif.setWind7v4(t.getWind74());
            dif.setWind10class(t.getWind10class());
            dif.setWind10v1(t.getWind101());
            dif.setWind10v2(t.getWind102());
            dif.setWind10v3(t.getWind103());
            dif.setWind10v4(t.getWind104());
            dif.setWind12class(t.getWind12class());
            dif.setWind12v1(t.getWind121());
            dif.setWind12v2(t.getWind122());
            dif.setWind12v3(t.getWind123());
            dif.setWind12v4(t.getWind124());
            dif.setWindv(t.getWind());
            resultList.add(dif);
        }
        return resultList;

    }

    @Override
    public List<Lonlat> findDataByRect(Lonlat maxLonlat, Lonlat minLonlat) {
        String sql="select lon,lat,'his' as ty,tfbh from histctrack where lat<="+maxLonlat.getLat()+" and lat>="+minLonlat.getLat()+" and lon<="+maxLonlat.getLon()+" and lon>="+minLonlat.getLon();
        Query queryForCount1 = entityManager.createNativeQuery(sql);
        List<Object[]> list=queryForCount1.getResultList();
        List<Lonlat> listLatLon=new ArrayList<>();
        if(list.size()>0){
            for (int i=0;i<list.size();i++){
                Lonlat lonlat=new Lonlat();
                lonlat.setLon(String.valueOf(list.get(i)[0]));
                lonlat.setLat(String.valueOf(list.get(i)[1]));
                lonlat.setTy(String.valueOf(list.get(i)[2]));
                if(list.get(i)[3]!=null){
                    lonlat.setTfbh(String.valueOf(list.get(i)[3]));
                }
                listLatLon.add(lonlat);
            }
        }
        return listLatLon;
    }

    @Override
    public List<SimpleTf> queryMaxWind(List<SimpleTf> list) {
        if(list!=null){
            List<SimpleTf> resultList = new ArrayList<>();
            Map<String,SimpleTf> tfMap = new HashedMap();
            StringBuffer tfbhStr = new StringBuffer();
            int len = list.size();
            for (int i=len-1;i>=0;i--) {
                String tfbh = list.get(i).getTfbh();
                if(i<len-1){
                    tfbhStr.append(",");
                }
                tfbhStr.append("'"+tfbh+"'");
                tfMap.put(tfbh,list.get(i));
            }
            StringBuffer hisSql = new StringBuffer("SELECT tfbh, max(wind) as maxwind FROM histctrack WHERE 1=1" );
            hisSql.append(" and tfbh in (");
            hisSql.append(tfbhStr.toString());
            hisSql.append(") GROUP BY tfbh order by datetime desc");

            StringBuffer realSql = new StringBuffer("SELECT tfbh, max(windv) as maxwind FROM tcrealtime WHERE 1=1");
            realSql.append(" and tfbh in ( ");
            realSql.append(tfbhStr.toString());
            realSql.append(" ) GROUP BY tfbh order by datetime desc");

            Query queryForHist= entityManager.createNativeQuery(hisSql.toString());
            List<Object[]> hisList = queryForHist.getResultList();
            Query queryForReal = entityManager.createNativeQuery(realSql.toString());
            List<Object[]> realList = queryForReal.getResultList();
            realList.addAll(hisList);

            for (Object[] obj: realList){
                String bh = obj[0].toString();
                String wind = obj[1].toString();

                SimpleTf  simpleTf = tfMap.get(bh);
                simpleTf.setMaxWind(wind);
                resultList.add(simpleTf);
            }
            return resultList;
        }
        return list;
    }

    @Override
    public Map<String, String> getStrengthStat(String substring) {
        Map<String, String> map = new HashMap<>();
        String sql = "SELECT count(strength) as count,strength from histctrack  where 1=1";
        sql+=" and tfbh in("+substring+")";
        sql += " GROUP BY strength";
        Query queryForcount = entityManager.createNativeQuery(sql);
        try {
            List<Object[]> temp = queryForcount.getResultList();
            if (temp.size() > 0) {
                for (int i = 0; i < temp.size(); i++) {
                    map.put(String.valueOf(temp.get(i)[1]), String.valueOf(temp.get(i)[0]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    private Map<String, Tcname> getStringTcnameMap() {
        Iterator<Tcname> tcnameIter = tcnameRepository.findAll().iterator();
        Map<String, Tcname> tcnameMap = new HashMap<>(140);

        while (tcnameIter.hasNext()) {
            Tcname tcname = tcnameIter.next();
            tcnameMap.put(tcname.getEngname().toUpperCase(), tcname);
        }
        return tcnameMap;
    }

    /**
     * 台风信息生成List
     *
     * @param tcnameMap
     * @param listReal
     * @return
     */
    private List<SimpleTf> getSimpleTfs(Map<String, Tcname> tcnameMap, List<Object[]> listReal) {
        List<SimpleTf> resultList = new ArrayList<>();
        for (Object[] objs : listReal) {
            SimpleTf simpleTf = new SimpleTf();
            simpleTf.setTfbh(objs[0].toString());
            simpleTf.setTy(objs[1].toString());
            String enName = objs[2].toString();
            simpleTf.setName(enName);
            String cnName = tcnameMap.get(enName.toUpperCase()) != null ? tcnameMap.get(enName.toUpperCase()).getChnname() : null;
            simpleTf.setChnname(cnName);
            resultList.add(simpleTf);
        }
        return resultList;
    }

    @Deprecated
    public List<SimpleTf> queryByParams(HistctrackParam histctrackParam) {
        try {
            StringBuffer sb = new StringBuffer("select DISTINCT a.name,CONCAT(date_format(a.datetime,'%Y'),a.tfxh) as xuhao,'his' as ty,if(b.chnname='' or b.chnname is null,'',chnname) chnname  from histctrack a left join tcname b on a.name=b.engname where 1=1 ");
            StringBuffer sb1 = new StringBuffer("select DISTINCT a.xuhao,a.tfbhbabj from tcrealtime a  where a.center='BABJ' and a.fcsthour=0 and  a.FCSTType='BABJWTPQ' ");
            if (!StringUtils.isEmpty(histctrackParam.getTfbhs())) {
                String tfbh = histctrackParam.getTfbhs();
                tfbh = tfbh.trim();
                if (tfbh.contains(";")) {
                    tfbh = tfbh.replace(";", ",");
                }
                if (tfbh.contains("；")) {
                    tfbh = tfbh.replace("；", ",");
                }
                List<String> list = new ArrayList<String>();
                if (tfbh.contains(",")) {
                    Collections.addAll(list, tfbh.split(","));
                } else {
                    list.add(tfbh);
                }

                Map<String, List<String>> maps = new HashMap();
                if (list.size() > 0) {
                    for (String one : list) {
                        //编码为四位数字
                        if (one.length() == 4) {
                            //如果是0开头的则表示2000年后
                            if (one.trim().startsWith("0") || one.trim().startsWith("1") || one.trim().startsWith("2") || one.trim().startsWith("3")) {
                                String newYear = "20" + one.substring(0, 2);
                                if (maps.containsKey(newYear)) {
                                    String temp = one.substring(2);
                                    maps.get(newYear).add(temp);
                                } else {
                                    List<String> listnew = new ArrayList();
                                    listnew.add(one.substring(2));
                                    maps.put(newYear, listnew);
                                }
                            } else {
                                String oldYear = "19" + one.substring(0, 2);
                                if (maps.containsKey(oldYear)) {
                                    maps.get(oldYear).add(one.substring(2));
                                } else {
                                    List<String> listnew = new ArrayList();
                                    listnew.add(one.substring(2));
                                    maps.put(oldYear, listnew);
                                }

                            }
                        }

                    }//加完了

                }
                String sqlPj = "";
                if (maps.size() > 0) {
                    Iterator it = maps.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry<String, List<String>> entry = (Map.Entry) it.next();
                        String year = entry.getKey();
                        List<String> numList = entry.getValue();
                        String yearbg = year + "-01-01 00:00:00";
                        String yeared = year + "-12-31 23:59:59";
                        //处理numList
                        String tfbhs = "";
                        if (numList.size() > 0) {
                            for (int i = 0; i < numList.size(); i++) {
                                int num = Integer.valueOf(numList.get(i));
                                if (i == 0) {
                                    tfbhs += "" + num;
                                } else {
                                    tfbhs += "," + num;
                                }

                            }
                        }
                        if (sqlPj.length() == 0) {
                            sqlPj += "and (a.datetime >='" + yearbg + "' and a.datetime <='" + yeared + "' and a.tfxh in(" + tfbhs + ") ) ";
                        } else {
                            sqlPj += " or (a.datetime >='" + yearbg + "' and a.datetime <='" + yeared + "' and a.tfxh in(" + tfbhs + ") ) ";
                        }

                    }
                }
                sb.append(sqlPj);
                sb1.append("and  a.tfbhbabj in(" + tfbh + ") ");
            }
            //时间比较
            if (!StringUtils.isEmpty(histctrackParam.getBeginTime()) || !StringUtils.isEmpty(histctrackParam.getEndTime())) {
                String begin = histctrackParam.getBeginTime();
                String end = histctrackParam.getEndTime();
                if (!StringUtils.isEmpty(begin)) {
                    sb.append(" and a.datetime >='" + begin + "'");
                    sb1.append(" and a.datetime >='" + begin + "'");
                }
                if (!StringUtils.isEmpty(end)) {
                    sb.append(" and a.datetime <='" + end + "'");
                    sb1.append(" and a.datetime <='" + end + "'");
                }
            } else {
                if (!StringUtils.isEmpty(histctrackParam.getYear())) {
                    String year = histctrackParam.getYear();//年代
                    String zzyear = year + "-01-01 00:00:00";//从一月一日
                    String zzyearEnd = year + "-12-31 23:59:59";//到
                    sb.append(" and a.datetime >='" + zzyear + "'");
                    sb.append(" and a.datetime <='" + zzyearEnd + "'");
                    sb1.append(" and a.datetime >='" + zzyear + "'");
                    sb1.append(" and a.datetime <='" + zzyearEnd + "'");
                }
            }


            //如果是登陆前的话
            if (!StringUtils.isEmpty(histctrackParam.getDlbefore())) {
                if (histctrackParam.getDlbefore().equals("true")) {//暂时设定 c_id为台风编号，如果是实时库的则按照实时库的xuhao 如果是历史库则按照历史库的datetime截取的年份+tfxh
                    String sqlDl = "select c.c_tfbh from tflandinginfo c where 1=1 ";
                    //根据气压 或者气压范围
                    if (!StringUtils.isEmpty(histctrackParam.getMaxpressure()) || !StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                        if (!StringUtils.isEmpty(histctrackParam.getMaxpressure())) {
                            sqlDl += " and c.v_centerpres<=" + histctrackParam.getMaxpressure();
                        }
                        if (!StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                            sqlDl += " and c.v_centerpres>=" + histctrackParam.getMinpressure();
                        }
                    }
                    //风速
                    if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity()) || !StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                        if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity())) {
                            sqlDl += " and c.c_windspeed<='" + histctrackParam.getMaxvelocity() + "'";
                        }
                        if (!StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                            sqlDl += " and c.c_windspeed>='" + histctrackParam.getMinvelocity() + "'";
                        }
                    }
                    //强度等级
                    if (!StringUtils.isEmpty(histctrackParam.getStrength())) {
                        sqlDl += " and c.c_windlevel in(" + histctrackParam.getVelocity() + ")";
                    }
                    sb.append("and CONCAT(date_format(a.datetime,'%Y'),a.tfxh) in(");
                    sb.append(sqlDl);
                    sb.append(")");
                    sb1.append("and a.tfbhbabj in(");
                    sb1.append(sqlDl);
                    sb1.append(")");
                }
            } else {
                //气压
                if (!StringUtils.isEmpty(histctrackParam.getMaxpressure()) || !StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                    if (!StringUtils.isEmpty(histctrackParam.getMaxpressure())) {
                        sb.append(" and a.pressure<='" + histctrackParam.getMaxpressure() + "'");
                        sb1.append(" and a.pressure<='" + histctrackParam.getMaxpressure() + "'");
                    }
                    if (!StringUtils.isEmpty(histctrackParam.getMinpressure())) {
                        sb.append(" and a.pressure>='" + histctrackParam.getMinpressure() + "'");
                        sb1.append(" and a.pressure>='" + histctrackParam.getMinpressure() + "'");
                    }
                }
                //风速范围
                if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity()) || !StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                    if (!StringUtils.isEmpty(histctrackParam.getMaxvelocity())) {
                        sb.append(" and a.wind<='" + histctrackParam.getMaxvelocity() + "'");
                        sb1.append(" and a.windv<='" + histctrackParam.getMaxvelocity() + "'");
                    }
                    if (!StringUtils.isEmpty(histctrackParam.getMinvelocity())) {
                        sb.append(" and a.wind>='" + histctrackParam.getMinvelocity() + "'");
                        sb1.append(" and a.windv>='" + histctrackParam.getMinvelocity() + "'");
                    }
                } else {
                    if (!StringUtils.isEmpty(histctrackParam.getVelocity())) {
                        sb.append(" and a.wind='" + histctrackParam.getVelocity() + "'");
                        sb1.append(" and a.windv='" + histctrackParam.getVelocity() + "'");
                    }
                }

                if (!StringUtils.isEmpty(histctrackParam.getStrength())) {
                    sb.append(" and a.strength in(" + histctrackParam.getStrength() + ")");
                    sb1.append(" and a.strength in(" + histctrackParam.getStrength() + ")");
                }

            }
            //根据经纬度定点查
            if (!StringUtils.isEmpty(histctrackParam.getLat()) && !StringUtils.isEmpty(histctrackParam.getLon()) && StringUtils.isEmpty(histctrackParam.getLlWidth())) {
                sb.append(" and a.lat like " + histctrackParam.getLat());
                sb.append(" and a.lon like " + histctrackParam.getLon());
                sb1.append(" and a.lat like " + histctrackParam.getLat());
                sb1.append(" and a.lon like " + histctrackParam.getLon());
            }
            //根据经纬度矩形查
            if (!StringUtils.isEmpty(histctrackParam.getMinlat())) {
                sb.append(" and a.lat>" + histctrackParam.getMinlat());
                sb1.append(" and a.lat>" + histctrackParam.getMinlat());
            }
            if (!StringUtils.isEmpty(histctrackParam.getMinlon())) {
                sb.append(" and a.lon>" + histctrackParam.getMinlon());
                sb1.append(" and a.lon>" + histctrackParam.getMinlon());
            }
            if (!StringUtils.isEmpty(histctrackParam.getMaxlat())) {
                sb.append(" and a.lat<" + histctrackParam.getMaxlat());
                sb1.append(" and a.lat<" + histctrackParam.getMaxlat());
            }
            if (!StringUtils.isEmpty(histctrackParam.getMaxlon())) {
                sb.append(" and a.lon<" + histctrackParam.getMaxlon());
                sb1.append(" and a.lon<" + histctrackParam.getMaxlon());
            }
            //如果同时存在经纬度和经纬度范围 则通过经纬度范围查
            if (!StringUtils.isEmpty(histctrackParam.getLat()) && !StringUtils.isEmpty(histctrackParam.getLon()) && !StringUtils.isEmpty(histctrackParam.getLlWidth())) {
                String sqlL = " and sqrt((((" + histctrackParam.getLon() + "-a.lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+a.lat)/2)*PI()/180)/180)*" +
                        "((" + histctrackParam.getLon() + "-a.lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+a.lat)/2)*PI()/180)/180))+" +
                        "(((" + histctrackParam.getLat() + "-a.lat)*PI()*12656/180)*((" + histctrackParam.getLat() + "-a.lat)*PI()*12656/180)))<" + histctrackParam.getLlWidth();
                sb.append(sqlL);
                sb1.append(sqlL);

            }
            sb.append(" order by datetime");
            sb1.append(" order by datetime");
            //根据前面的条件从历史库中查询数据
            Query queryForCount = entityManager.createNativeQuery(sb.toString());
            List<Object[]> adminList = queryForCount.getResultList();
            //查询实时库中的编号对应的 序列号
            Query queryForCount2 = entityManager.createNativeQuery(sb1.toString());
            List<Object[]> adminList2 = queryForCount2.getResultList();
            List<Object[]> adminListall = new ArrayList<>();
            if (adminList.size() > 0) {
                adminListall.addAll(adminList);
            }
            if (adminList2 != null && adminList2.size() > 0) {//如果序号和编号都存在的话
                for (int i = 0; i < adminList2.size(); i++) {
                    //先取出序号
                    String xuhao = String.valueOf(adminList2.get(i)[0]);
                    String bjtfbh = String.valueOf(adminList2.get(i)[1]);
                    if (!xuhao.endsWith("0000")) {//筛选序号 如果序号以0000结尾则表示为垃圾数据排除掉
                        //根据序号查出改序号最大日期的
                        String SQL = "SELECT max(datetime) datetime from tcrealtime WHERE xuhao='" + xuhao + "'AND tfbhbabj='" + bjtfbh + "' AND center='BABJ' AND FCSTType='BABJWTPQ' ";
                        Query queryForCount3 = entityManager.createNativeQuery(SQL);
                        List<String> temp = queryForCount3.getResultList();
                        String maxtime = temp.get(0);//获取最大的时间
                        String maxYear = maxtime.substring(0, 4);
                        Calendar a = Calendar.getInstance();
                        int year = a.get(Calendar.YEAR);
                        if (year - Integer.valueOf(maxYear) < 2) {//根据查询出来的时间来判断是否是近两年的数据如果是近两年则查出来对应的台风记录
                            String SQL2 = "SELECT if(engname='' or engname is null,'NAMELESS',engname) engname,xuhao,'new' as ty,if(chnname ,'NAMELESS',chnname) chnname,tfbhbabj  from tcrealtime a WHERE center='BABJ' AND fcsthour%24=0  AND FCSTType='BABJWTPQ'" +
                                    "  AND datetime='" + maxtime + "' AND xuhao=" + xuhao + " limit 0,1 ";
                            Query queryForCount4 = entityManager.createNativeQuery(SQL2);
                            List<Object[]> temp2 = queryForCount4.getResultList();
                            if (temp2 != null && temp2.size() > 0) {
                                adminListall.addAll(temp2);
                            }
                        }


                    }
                }
            }
            List<SimpleTf> list = new ArrayList<>();
            //将记录整编为一个对象列表
            if (adminListall != null && adminListall.size() > 0) {
                for (Object[] o : adminListall) {
                    int len = o.length;
                    SimpleTf simpleTf = new SimpleTf();
                    for (int i = 0; i < len; i++) {
                        if (i == 0) {
                            simpleTf.setName((String) o[i]);//台风名称
                        }
                        if (i == 1) {
                            simpleTf.setXuhao(String.valueOf(o[i]));//台风序号
                        }

                        if (i == 2) {
                            String ty = (String) o[i];
                            simpleTf.setTy(ty);
                            if (ty.equals("his")) {//如果台风为历史数据则转换一下
                                String xuh = simpleTf.getXuhao();
                                String q2 = xuh.substring(2, 4);
                                String e = xuh.substring(4);
                                if (e.length() == 1) {
                                    e = "0" + e;
                                }
                                String tfbhs = q2 + e;
                                simpleTf.setTfbh(tfbhs);//加入转换后的编号
                            }
                        }
                        if (i == 3) {
                            String cname = (String) o[i];
                            if (!StringUtils.isEmpty(cname)) {
                                simpleTf.setChnname(new String(cname.getBytes("8859_1"), "GB2312"));//中文名称 需要转码
                            }
                        }
                        if (i == 4) {//只有实时库里面会有第五条记录所以给加上对应编号
                            String bh = String.valueOf(o[i]);
                            simpleTf.setTfbh(bh);
                        }
                    }
                    list.add(simpleTf);
                }
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过登陆城市来获取台风记录
     *
     * @param histctrackParam
     * @return
     */
    private void addCitySqlStr(StringBuffer strBufReal, StringBuffer strBufHis, HistctrackParam histctrackParam) {
        //登陆城市点查询
        StringBuffer sqlb = new StringBuffer("select t from Tflandinginfo t where 1=1 ");//通过登陆信息表来获取登陆台风的编号
        String landCity = histctrackParam.getLandCity();
        if (!StringUtils.isEmpty(landCity)) {
            sqlb.append(" and (");
            sqlb.append(" t.cDlprovince1='" + landCity + "'");
            sqlb.append(" or t.cDlprovince2='" + landCity + "'");
            sqlb.append(" or t.cDlcity1='" + landCity + "'");
            sqlb.append(" or t.cDlcity2='" + landCity + "'");
            sqlb.append(" )");
        } else {
            if (!StringUtils.isEmpty(histctrackParam.getDlprovince1())) {//判断登陆省份是否存在

                sqlb.append(" and t.cDlprovince1='" + histctrackParam.getDlprovince1() + "'");
            }
            if (!StringUtils.isEmpty(histctrackParam.getDlprovince2())) {//判断登陆省份2是否存在
                sqlb.append(" and t.cDlprovince2='" + histctrackParam.getDlprovince2() + "'");
            }
            if (!StringUtils.isEmpty(histctrackParam.getDlcity1())) {//判断城市1是否存在
                sqlb.append(" and t.cDlcity1='" + histctrackParam.getDlcity1() + "'");
            }
            if (!StringUtils.isEmpty(histctrackParam.getDlcity2())) {//判断城市2是否存在
                sqlb.append(" and t.cDlcity2='" + histctrackParam.getDlcity2() + "'");
            }
        }

            String listString = "";//或者台风的编号 用逗号隔开
            if(!StringUtils.isEmpty(histctrackParam.getDlprovince1())||!StringUtils.isEmpty(histctrackParam.getDlprovince2())||!StringUtils.isEmpty(histctrackParam.getDlcity1())||!StringUtils.isEmpty(histctrackParam.getDlcity2())||!StringUtils.isEmpty(histctrackParam.getLandCity())){
                Query queryForCount1 = entityManager.createQuery(sqlb.toString(), Tflandinginfo.class);

                List<Tflandinginfo> list2 = queryForCount1.getResultList();
                if (list2 != null && list2.size() > 0) {
                    for (int i = 0; i < list2.size(); i++) {
                        Tflandinginfo tf = list2.get(i);
                        String tfbh = tf.getCTfbh();
                        if (!StringUtils.isEmpty(tfbh) && tfbh.length() == 4) {
                            if (i == 0) {
                                listString += tf.getCTfbh();//获取台风编号用逗号隔开
                            } else {
                                listString += "," + tf.getCTfbh();
                            }
                        }
                    }
                }
                listString = listString.length() > 0 ? listString : "-9999";
                strBufReal.append(" and t.tfbh in (" + listString + ")");
                strBufHis.append(" and t.tfbh in (" + listString + ")");
            }



    }

    /**
     * 该方法暂时没用
     *
     * @param histctrackParam
     * @return
     */
    @Override
    public Object findTfInfo(HistctrackParam histctrackParam) {
        try {
            if (histctrackParam.getTy().equals("his")) {
                StringBuffer sb = new StringBuffer("select a.*,b.chnname  from histctrack a left join tcname b on a.name=b.engname where a.tfxh='" + histctrackParam.getXuhao() + "' and  a.fcsthour=0 ");
                Query queryForCount1 = entityManager.createNativeQuery(sb.toString(), Histctrack_Tcname.class);
                List<Histctrack_Tcname> list2 = queryForCount1.getResultList();
                return list2;
            } else if (histctrackParam.getTy().equals("new")) {
                StringBuffer sb1 = new StringBuffer("select a.* from tcrealtime a  where  a.center='BABJ' and a.bwtype='WTPQ20' and a.xuhao='" + histctrackParam.getXuhao() + "'  and a.fcsthour=0   ");
                if (!StringUtils.isEmpty(histctrackParam.getTfname())) {
                    sb1.append(" and a.engname='" + histctrackParam.getTfname() + "'");
                }
                Query queryForCount1 = entityManager.createNativeQuery(sb1.toString(), Tcrealtime.class);
                List<Tcrealtime> list2 = queryForCount1.getResultList();
                return list2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 根据编号获取台风路径
     *
     * @param tfbh 台风编号
     * @return
     */
    @Override
    public List<DateTf> findPathData(String tfbh) {

        StringBuffer sb = new StringBuffer("select a.*,b.chnname  from histctrack a left join tcname b on a.name=b.engname where a.tfbh='" + tfbh + "' ");
        sb.append("order by  a.datetime ");
        Query queryForCount1 = entityManager.createNativeQuery(sb.toString(), Histctrack_Tcname.class);
        List<Histctrack_Tcname> list2 = queryForCount1.getResultList();

        List<DateTf> resultList = new ArrayList<DateTf>();
        // 处理数据
        int n = list2.size();
        //整合为与实时库一样的字段名称 维持统一性
        for (int i = 0; i < n; i++) {
            Histctrack_Tcname t = list2.get(i);
            DateTf dif = new DateTf();
            dif.setId(t.getId());
            dif.setChnname(t.getChnname());
            dif.setTfbh(t.getTfbh());
            dif.setCenter(t.getCenter());
            dif.setDatetime(t.getDatetime());
            dif.setCreateTime(t.getCreateTime());
            dif.setEngname(t.getName());
            dif.setCreateTime(t.getCreateTime());
            dif.setFcsthour(0);
            dif.setFCSTType("");
            dif.setTfbhbabj("");
            dif.setTfxh(t.getTfxh());
            dif.setLat(t.getLat());
            dif.setLon(t.getLon());
            dif.setMemo(t.getMemo());
            dif.setMovedir(t.getDirection());
            dif.setPressure(t.getPressure());
            dif.setStrength(t.getStrength());
            dif.setTsCreated(t.getCreateTime());
            dif.setWind7class(t.getWind7class());
            dif.setWind7v1(t.getWind71());
            dif.setWind7v2(t.getWind72());
            dif.setWind7v3(t.getWind73());
            dif.setWind7v4(t.getWind74());
            dif.setWind10class(t.getWind10class());
            dif.setWind10v1(t.getWind101());
            dif.setWind10v2(t.getWind102());
            dif.setWind10v3(t.getWind103());
            dif.setWind10v4(t.getWind104());
            dif.setWind12class(t.getWind12class());
            dif.setWind12v1(t.getWind121());
            dif.setWind12v2(t.getWind122());
            dif.setWind12v3(t.getWind123());
            dif.setWind12v4(t.getWind124());
            dif.setWindv(t.getWind());
            resultList.add(dif);
        }
        return resultList;
    }

    /**
     * 获取最大台风风速记录
     *
     * @param histctrackParam
     * @return
     */
    @Override
    public Map<String, String> getMaxFs(HistctrackParam histctrackParam) {
        Map<String, String> map = new LinkedHashMap<>();
        String sql = "SELECT MAX(wind) as winv,DATE_FORMAT(datetime,'%Y') as date FROM histctrack";
        //如果经纬度 和经纬度范围半径都存在的话进行筛选
        if (!StringUtils.isEmpty(histctrackParam.getLon()) && !StringUtils.isEmpty(histctrackParam.getLon()) && !StringUtils.isEmpty(histctrackParam.getLlWidth())) {
            sql += "  where sqrt((((" + histctrackParam.getLon() + "-lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+lat)/2)*PI()/180)/180)*" +
                    "((" + histctrackParam.getLon() + "-lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+lat)/2)*PI()/180)/180))+" +
                    "(((" + histctrackParam.getLat() + "-lat)*PI()*12656/180)*((" + histctrackParam.getLat() + "-lat)*PI()*12656/180)))<" + histctrackParam.getLlWidth();
        }
        sql += "  GROUP BY DATE_FORMAT(datetime,'%Y')";
        Query queryForcount = entityManager.createNativeQuery(sql);
        try {
            List<Object[]> temp = queryForcount.getResultList();
            if (temp.size() > 0) {
                for (int i = 0; i < temp.size(); i++) {
                    //筛选的结果为年 和 最大风速
                    map.put(String.valueOf(temp.get(i)[1]), String.valueOf(temp.get(i)[0]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 获取台风强度统计
     *
     * @param histctrackParam
     * @return
     */
    @Override
    public Map<String, String> getMaxStrength(HistctrackParam histctrackParam) {
        Map<String, String> map = new HashMap<>();
        String sql = "SELECT count(strength) as count,strength from histctrack  ";
        //如果经纬度 和经纬度范围半径都存在的话进行筛选
        if (!StringUtils.isEmpty(histctrackParam.getLon()) && !StringUtils.isEmpty(histctrackParam.getLon()) && !StringUtils.isEmpty(histctrackParam.getLlWidth())) {
            sql += "  where sqrt((((" + histctrackParam.getLon() + "-lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+lat)/2)*PI()/180)/180)*" +
                    "((" + histctrackParam.getLon() + "-lon)*PI()*12656*cos(((" + histctrackParam.getLat() + "+lat)/2)*PI()/180)/180))+" +
                    "(((" + histctrackParam.getLat() + "-lat)*PI()*12656/180)*((" + histctrackParam.getLat() + "-lat)*PI()*12656/180)))<" + histctrackParam.getLlWidth();
        }
        sql += " GROUP BY strength";
        Query queryForcount = entityManager.createNativeQuery(sql);
        try {
            List<Object[]> temp = queryForcount.getResultList();
            if (temp.size() > 0) {
                for (int i = 0; i < temp.size(); i++) {
                    map.put(String.valueOf(temp.get(i)[1]), String.valueOf(temp.get(i)[0]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }


        }
